<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/user.jsp"%>
<%!
	// 通知列表
	String list(Input input, Integer uid) {
		Integer page = 1; // 页码
		Integer hospital = input.getInt("hospital", 20); // 每一页条目
		Integer itemCount = input.getInt("item_count", 20); // 每一页条目
		String appType = input.getDesc("app_type", "hospital"); // 版本类型
		String sinceID = input.getDesc("since_id", "0"); // 下拉刷新
		String maxID = input.getDesc("max_id", "0"); // 上拉加载
		String date = input.getDesc("date", "");
		String type = input.getDesc("type", "1"); // 通知分类, 事务通知2 通知患者3
		// 查看某天的通知
		if (!date.equals("")) {
			date = " and concat(FROM_UNIXTIME(n.cTime/1000,'%Y-%m-%d'), '') = '" + date + "'";
		}
		String order = "desc"; // 排序
		String id = "";
		Message msg = new Message(0);
		if (!sinceID.equals("0")) {
			id = " and n.id > " + sinceID;
			order = "asc";
		} 
		if (!maxID.equals("0")) {
			id = " and n.id < " + maxID;
		}
		String hql = "select new map(n.id as id, n.title as title, n.content as content, concat(FROM_UNIXTIME(n.cTime/1000,'%Y-%m-%d %H:%i:%s'), '')  as create_time, nc.isConfirmed as is_confirmed, case when n.imgs is null then '[]' else imgs end as imgs, case when n.voices is null then '[]' else voices end as voices,n.sender as sender) from  Notification as n, NotificationConfirm as nc where nc.nid=n.id and nc.uid=" + uid + id  + " and n.hospital=" + hospital + " and n.status=" + STATUS_ENABLE + " and n.type=" + type + " and nc.status= " + STATUS_ENABLE + date + id  +  "  order by n.id " + order;
		List<Map> notifictionList = QBuilder.db("basedb").queryHql(hql, itemCount, page);
		if (isEmpty(notifictionList)) {
			msg.setErrcodeAndErrmsg(30041, "没有更多通知");
			return msg.toJson();
		}
		if (order.equals("asc")) { 
			Collections.reverse(notifictionList);
		}
		List<Integer> list = new ArrayList<Integer>();
		for (Map map:notifictionList) {
			list.add(Tools.getInt(map.get("sender")+""));
		}
		Map<Integer, User> userMap = usersByUids(list);
		for (Map map:notifictionList) {
			User user = userMap.get(Tools.getInt(map.get("sender")+""));
			if (user != null) {
				map.put("sender_name", user.getName());
				map.put("sender_avatar", user.getAvatar());
				map.put("sender_title", user.getTitle());
			}
		}
		msg.put("data", notifictionList);
		return msg.toJson();
	}
%>
<%
	Tools.encryptPrint(out, list(input, token.getUid()));
%>
